//
//  RootViewController.m
//  TarPhone
//
//  Created by Christopher Anderson on 15/03/2009.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//

#import "RootViewController.h"
#import "TarPhoneAppDelegate.h"
#import "MPOAuthMultiPartImageRequestParameter.h"
#import "MPOAuthMultiPartTextRequestParameter.h"
#import "MPOAuthMultipartRequest.h"
#import "WorkflowProvider.h"
#import "Workflow.h";
#import "MPOAuthAPI.h"
#import "WorkflowTableCell.h"
#import "WorkflowViewController.h"



@implementation RootViewController



@synthesize workflowProvider;
@synthesize workflows;
@synthesize workflowViewController;

- (void)viewDidLoad {
    [super viewDidLoad];
	workflowProvider = [[WorkflowProvider alloc] init];
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	[self startBackgroundOperation];
}


- (NSOperation*) getOperation {
	return [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadWorkflows) object:nil];
}

- (void) loadWorkflows {
	workflows = [workflowProvider getWorkFlows];
	NSLog(@"Workflows found %d", [workflows count]);
	[self performSelectorOnMainThread:@selector(loadComplete) withObject:nil waitUntilDone:YES];
}

- (void) loadComplete {
	if (workflows != nil) {
		if ([workflows count] > 0 ) {
			[self restoreTableDelegate];
			[myTableView flashScrollIndicators];
		} else {
			[self setInformationReportDelegateForTableView:@"No workflows found"];
		}
	} else {
		[self reportConnectionProblem];
	}
	
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
 */

- (void)_methodLoadedFromURL:(NSURL *)inURL withResponseString:(NSString *)inString {
	textOutput.text = inString;
	NSLog(@"Returned from web service: %@", inString);
}

- (void)methodEntered:(UITextField *)inTextField {
	//[_oauthAPI performMethod:inTextField.text withTarget:self andAction:@selector(_methodLoadedFromURL:withResponseString:)];
}


/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [workflows count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"WorkflowCell";

	cell = (WorkflowTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (cell == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"WorkflowTableCell" owner:self options:nil];
	}
	Workflow *workflow = [workflows objectAtIndex:indexPath.row];
	[cell setContents:workflow];
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if (workflowViewController == nil) {
		workflowViewController = [[WorkflowViewController alloc] initWithNibName:@"WorkflowViewController" bundle:nil];
	}
	
	Workflow *workflow = [workflows objectAtIndex:indexPath.row];
	[workflowViewController setWorkflow:workflow];
	[self.navigationController pushViewController:workflowViewController animated:YES];
	 
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
}


@end

